package com.example.user;

import com.alibaba.fastjson.JSONObject;
import com.example.Configuration;
import com.example.utils.HttpUtils;
import org.apache.http.HttpEntity;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.util.ArrayList;

/**
 * 用户信息处理类
 */
public class UserHandler {

    /**
     * 店主登录 获取店铺信息
     * @param userName 用户名
     * @param password 密码
     * @return 返回查询到的店铺ID用于接收RabbitMQ队列中的消息
     */
    public static String login(String userName,String password)throws Exception{
        if(userName == null || password == null){
            return null;
        }
        String shopId = null;
        //发起登录校验
        ArrayList<BasicNameValuePair> params = new ArrayList<>();
        params.add(new BasicNameValuePair("userName",userName));
        params.add(new BasicNameValuePair("password",password));
        String httpEntity = HttpUtils.post(Configuration.LOGIN_URI,params);
        //解析HttpEntity 判断是否登录成功
        if(httpEntity != null){
            JSONObject jsonObject = HttpUtils.parseResponse(httpEntity);
            JSONObject dataObject = HttpUtils.parseResponse(jsonObject);
            shopId = dataObject.getString("shop_id");
        }
        return shopId;
    }
}
